#pragma once

#include <osg/ref_ptr>
#include <osg/Node>
#include <osg/MatrixTransform>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osgDB/Registry>
#include <osgDB/ReaderWriter>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgManipulator/Translate2DDragger>
#include <osgManipulator/CommandManager>

//X3D-UML Stuff
#include "DiagramInterface.h"
//#include "TextView.h"
//#include "DiagramView.h"
//#include "StateView.h"
//#include "PseudoStateView.h"
//#include "TransitionView.h"
//#include "IDiagram.h"

// X3DUMLv2Ctrl.h : Declaration of the CX3DUMLv2Ctrl ActiveX Control class.


// CX3DUMLv2Ctrl : See X3DUMLv2Ctrl.cpp for implementation.

class CX3DUMLv2Ctrl : public COleControl
{
	DECLARE_DYNCREATE(CX3DUMLv2Ctrl)

// Constructor
public:
	CX3DUMLv2Ctrl();

// Overrides
public:
	virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
	virtual void DoPropExchange(CPropExchange* pPX);
	virtual void OnResetState();

// Implementation
protected:
	~CX3DUMLv2Ctrl();

	DECLARE_OLECREATE_EX(CX3DUMLv2Ctrl)    // Class factory and guid
	DECLARE_OLETYPELIB(CX3DUMLv2Ctrl)      // GetTypeInfo
	DECLARE_PROPPAGEIDS(CX3DUMLv2Ctrl)     // Property page IDs
	DECLARE_OLECTLTYPE(CX3DUMLv2Ctrl)		// Type name and misc status

// Message maps
	DECLARE_MESSAGE_MAP()

// Dispatch maps
	DECLARE_DISPATCH_MAP()

// Event maps
	DECLARE_EVENT_MAP()

// Dispatch and event IDs
public:
	enum {
		dispidLinkElementToDiagram = 14L,
		dispidSaveToOSG = 13L,
		dispidAddDiagram = 12L,
		dispidCurrentDiagramFullyQualifiedName = 11L,
		dispidSetOrCreateCurrentDiagram = 10L,
		dispidCurrentDiagramName = 9L,
		dispidShowSplashScreen = 8L,
		dispidSetCurrentDiagram = 7L,
		dispidAddTransitionView = 6L,
		dispidAddPseudoStateView = 5L,
		dispidRenderDiagram = 4L,
		dispidAddTextView = 3L,
		dispidAddStateView = 2L,
		dispidFrameUpdate = 1L
	};

private:
	osg::ref_ptr<osg::Geode> createPlane( float length, float width, osg::Image* image );
	osg::ref_ptr<osg::MatrixTransform> m_RootTrans;
	osg::ref_ptr<osg::Group> m_Root;
	osg::ref_ptr<osg::GraphicsContext::Traits> m_Traits;
	osgViewer::Viewer* m_Viewer;
	X3DUML::DiagramInterface* interactiveDiagram;
	char* m_Filename;

public:
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnDestroy();
	afx_msg void OnPaint();

protected:
	void LoadFile(LPCTSTR lpszName);
	void FrameUpdate(void);
	BSTR AddStateView(DOUBLE Width, DOUBLE Height, DOUBLE X, DOUBLE Y, DOUBLE Z);
	BSTR AddTextView(LPCTSTR TextString, DOUBLE X, DOUBLE Y, DOUBLE Z);
	BSTR AddDiagram(LPCTSTR TextString);
	void RenderDiagram(void);
	BSTR AddPseudoStateView(DOUBLE Width, DOUBLE Height, DOUBLE X, DOUBLE Y, DOUBLE Z, LPCTSTR PseudoStateType);
	BSTR AddTransitionView(LPCTSTR WayPointsCSVString);
	void SaveToOSG(LPCTSTR TextString);
private:
	void SplashScreen(void);
protected:
	VARIANT_BOOL SetCurrentDiagram(LPCTSTR FullQualifiedName);
private:
	//X3DUML::DiagramView* getDiagramByName(X3DUML::DiagramView* theDiagram, string fullyQualifiedName);
protected:
	void ShowSplashScreen(void);
	BSTR CurrentDiagramName(void);
	VARIANT_BOOL SetOrCreateCurrentDiagram(LPCTSTR FullQualifiedName);
	BSTR CurrentDiagramFullyQualifiedName(void);
	VARIANT_BOOL LinkElementToDiagram(LPCTSTR elementFullyQualifiedName, LPCTSTR diagramFullyQualifiedName);
};

